package com.sgb.merchant.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.merchant.api.MerchantsNet;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.BaseViewModel;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Call;

public class InvestmentApplyViewModel extends BaseViewModel {
   public MutableLiveData<Boolean> ldResult=new MutableLiveData<>();
   public MutableLiveData<String> ldUrl=new MutableLiveData<>();
   public MutableLiveData<String> ldNo=new MutableLiveData<>();

    public InvestmentApplyViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void uploadFile(File file) {
        try {
            Map<String, RequestBody> photos = new HashMap<>();
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            String fileName = URLEncoder.encode(file.getName(), "UTF-8");
            photos.put("file" + "\"; filename=\"" + fileName, requestBody);
            MerchantsNet.getInstance().getService().addFile(photos).enqueue(new ICallBack<BaseEntity>() {
                @Override
                public void onSuccess(BaseEntity data) {
                    if (data.isSuccess()) {
                        ldUrl.postValue(data.getData().toString());
                    }
                }

                @Override
                public void onFailure(Call<BaseEntity> call, Throwable t) {
                    super.onFailure(call, t);
                    //ldResult.postValue(false);
                }

            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public void addApply(Map<String,Object> map){
        MerchantsNet.getInstance().getService().addApply(map).enqueue(new ICallBack<BaseEntity<String>>() {
            @Override
            public void onSuccess(BaseEntity<String> data) {
                if(data.isSuccess()){
                    ldNo.postValue(data.getData());
                }
            }
        });
    }

}
